package operations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    public void work(BookList bookList) {
        System.out.println("删除图书！");

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除图书的名字:>");
        String delName = scanner.nextLine();

        //这里是bookList中创建delBooks方法，来实现删除
        //也可以index=-1来检查是否有要删除的数，然后就在这个work方法里删除
        int currentUserSide = bookList.getUsedSize();
        for (int i = 0; i < currentUserSide; i++) {
            //BookList中books被封装后，不能直接去访问books[i]，只能在BookList中设置一个方法返回books[i]，再遍历
            Book delBook = bookList.getPosBook(i);

            //找到书
            if(delName.equals(delBook.getName())){
                bookList.delBooks(bookList,i);
                System.out.println("删除图书成功！");
                bookList.setUsedSize(currentUserSide-1);
                return;
            }
        }
        //遍历完没找到
        System.out.println("没有你要删除的图书！");
    }
}
